package com.sunlg.smarthome.ui.wedget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.utils.DisplayUtils;

/**
 * Created by sunlg on 2017/4/25.
 */

public class CurtainView extends View {
    private Context mContext;

    private static final int DEFAULT_CURTAIN_COLOR = Color.GRAY;
    private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final float DEFAULT_TEXT_SIZE = 80.0f;

    private int mCurtainColor;
    private int mTextColor;
    private float mTextSize;
    private Paint mCurtainPaint;
    private Paint mTextPaint;

    private Path mCurtainLeftPath;
    private Path mCurtainRightPath;

    private int mCurtainWidth;
    private int mCurtainHeight;
    private int mCurtainWidthHalf;

    private int progress = 0;
    private int width;
    private int height;

    public CurtainView(Context context) {
        this(context, null);
    }

    public CurtainView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CurtainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        init();
    }

    private void init(){

        mCurtainPaint = new Paint();
        mCurtainPaint.setStyle(Paint.Style.FILL);
        mCurtainPaint.setAntiAlias(true);
        mCurtainPaint.setColor(DEFAULT_CURTAIN_COLOR);

        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(DEFAULT_TEXT_COLOR);
        mTextPaint.setTextSize(DEFAULT_TEXT_SIZE);

        mCurtainLeftPath = new Path();
        mCurtainRightPath = new Path();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = measureWidth(widthMeasureSpec);
        int h = measureHeight(heightMeasureSpec);

        setMeasuredDimension(w, h);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        width = getWidth();
        height = getHeight();

        mCurtainWidth = width - getPaddingLeft() - getPaddingRight();
        mCurtainHeight = height - getPaddingTop() - getPaddingBottom();
        mCurtainWidthHalf = mCurtainWidth/2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //清除之前画布中的路径
        mCurtainLeftPath.reset();
        mCurtainRightPath.reset();

        canvas.drawPath(getCurtainLeftPath(), mCurtainPaint);
        canvas.drawPath(getCurtainRightPath(), mCurtainPaint);

        canvas.drawText(progress+"%",
                getPaddingLeft()+mCurtainWidthHalf,
                getPaddingTop()+mCurtainHeight - mCurtainHeight/3,
                mTextPaint);
    }

    /**
     * 设置窗帘展开百分比
     * @param progress
     */
    public void setProgress(int progress){
        if(progress <= 100){
            this.progress = progress;
            invalidate();
        }
    }

    /**
     * 设置窗帘颜色
     * @param color
     */
    public void setCurtainColor(int color){
        mCurtainPaint.setColor(color);
    }

    /**
     * 设置字体颜色
     * @param color
     */
    public void setTextColor(int color){
        mTextPaint.setColor(color);
    }

    /**
     * 设置字体大小
     * @param size 单位sp
     */
    public void setTextSize(float size){
        mTextPaint.setTextSize(DisplayUtils.sp2px(this.mContext, size));
    }

    /**
     * 获取两点之间的中点
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
    private Point getCenterPoint(int x1, int y1, int x2, int y2){
        int x = (x1 + x2)/2;
        int y = (y1 + y2)/2;
        return new Point(x, y);
    }

    private Path getCurtainLeftPath(){
        //左边窗帘原点
        int x = getPaddingLeft();
        int y = getPaddingTop();
        //左边窗帘point1
        int x1 = x + mCurtainWidthHalf - (mCurtainWidthHalf/100) * progress / 4;
        int y1 = y;
        //左边窗帘point3
        int x3 = x + mCurtainWidthHalf - (mCurtainWidthHalf/100) * progress;
        int y3 = y + mCurtainHeight;
        //左边窗帘point2
        Point cp = getCenterPoint(x1, y1, x3, y3);
        int x2 = cp.x;
        int y2 = cp.y;

        Point cp1 = getCenterPoint(x1, y1, x2, y2);

        Point cp2 = getCenterPoint(x2, y2, x3, y3);

        //左边窗帘point4
        int x4 = x;
        int y4 = y + mCurtainHeight;

        mCurtainLeftPath.moveTo(x, y);
        mCurtainLeftPath.lineTo(x1, y1);
        mCurtainLeftPath.cubicTo(cp1.x , cp1.y - cp1.y/2,
                cp1.x, cp1.y + cp1.y/4,
                cp.x, cp.y);
        mCurtainLeftPath.cubicTo(cp2.x, cp2.y - cp2.y/2,
                cp2.x, cp2.y + cp2.y/4,
                x3, y3);
        mCurtainLeftPath.lineTo(x4, y4);
        mCurtainLeftPath.close();
        return mCurtainLeftPath;
    }

    private Path getCurtainRightPath(){
        //右边窗帘原点
        int x = mCurtainWidth + getPaddingLeft();
        int y = getPaddingTop();
        //右边窗帘point1
        int x1 = getPaddingLeft() + mCurtainWidthHalf + (mCurtainWidthHalf/100) * progress / 4;
        int y1 = y;

        //右边窗帘point3
        int x3 = getPaddingLeft() + mCurtainWidthHalf + (mCurtainWidthHalf/100) * progress;
        int y3 = y + mCurtainHeight;
        //右边窗帘point2
        Point cp = getCenterPoint(x1, y1, x3, y3);
        int x2 = cp.x;
        int y2 = cp.y;

        Point cp1 = getCenterPoint(x1, y1, x2, y2);

        Point cp2 = getCenterPoint(x2, y2, x3, y3);

        //左边窗帘point4
        int x4 = x;
        int y4 = y + mCurtainHeight;

        mCurtainRightPath.moveTo(x, y);
        mCurtainRightPath.lineTo(x1, y1);
        mCurtainRightPath.cubicTo(cp1.x, cp1.y - cp1.y/2,
                cp1.x, cp1.y + cp1.y/4,
                cp.x, cp.y);
        mCurtainRightPath.cubicTo(cp2.x, cp2.y - cp2.y/2,
                cp2.x, cp2.y + cp2.y/4,
                x3, y3);
        mCurtainRightPath.lineTo(x4, y4);
        mCurtainRightPath.close();

        return mCurtainRightPath;
    }

    private int measureWidth(int measureSpec) {
        int result = 200;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = specSize;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.min(result, specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int result = 200;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = specSize;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.min(result, specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }
}
